package com.wanqing.controller.user;

import com.wanqing.context.BaseContext;
import com.wanqing.dto.UserVoucherPageQueryDTO;
import com.wanqing.dto.VoucherReceiveDTO;
import com.wanqing.result.PageResult;
import com.wanqing.result.Result;
import com.wanqing.service.VoucherService;
import com.wanqing.vo.VoucherVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户优惠券接口（用户端）
 */
@RestController
@RequestMapping("/user/voucher")
@Tag(name = "C端-用户优惠券接口")
public class UserVoucherController {

    @Resource
    private VoucherService voucherService;

    /**
     * 用户端展示优惠券
     */
    @GetMapping("/list")
    @Operation(summary = "用户端展示优惠券")
    public Result<List<VoucherVO>> getVoucherListUser() {
        List<VoucherVO> list = voucherService.getVoucherListUser();
        return Result.success(list);
    }

    /**
     * 用户领取优惠券
     */
    @PostMapping("/receive")
    @Operation(summary = "用户领取优惠券")
    public Result<String> receive(@RequestBody @Valid VoucherReceiveDTO dto) {
        voucherService.receiveVoucher(dto);
        return Result.success();
    }

    /**
     * 查询用户优惠券列表
     */
    @GetMapping("/list/user")
    @Operation(summary = "查询用户优惠券列表")
    public Result<PageResult> getUserVoucherList(UserVoucherPageQueryDTO userVoucherPageQueryDTO) {
        userVoucherPageQueryDTO.setUserId(BaseContext.getCurrentId());
        PageResult list = voucherService.listUserVouchers(userVoucherPageQueryDTO);
        return Result.success(list);
    }

    /**
     * 使用优惠券（关联订单）
     */
    @Operation(summary = "使用优惠券（关联订单）")
    @PutMapping("/use")
    public Result useVoucher(@RequestParam Long id, @RequestParam Long orderId) {
        voucherService.useVoucher(id, orderId);
        return Result.success();
    }
}